Fix use-after-free bug in postgres example (14-02.zig)#127
Merged
Conversation
The `color_id` slice pointed into PGresult memory that was freed by `defer c.PQclear(res)` when the `blk:` block exited via `break`. On macOS the freed memory is zeroed, causing an empty string to be passed as `color_id` to the next PQexecPrepared call, which then failed with "invalid input syntax for type integer: ''". Fix: remove the blk: scope and defer PQclear(color_res) at the outer inline for loop level, so color_id stays valid for the inner loop that uses it.
Copilot
AI
changed the title
[WIP] Fix failing GitHub Actions job examples (macos-latest)
Fix use-after-free bug in postgres example (14-02.zig)
Jul 16, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a use-after-free in the PostgreSQL example by ensuring the PGresult backing color_id remains alive while it’s used to insert related rows.
Changes:
- Removes the
blk:scope that causedPQclearto run beforecolor_idwas consumed. - Defers
PQclear(color_res)to the outerinline foriteration socolor_idstays valid through the inner loop.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Change httpbin request from HTTP to HTTPS to ensure secure connection. Ignore responses with non-OK status to avoid intermittent timeouts from httpbin. This improves reliability and prevents unnecessary test failures caused by external service instability.
jiacai2050
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
examples (macos-latest)CI job was failing inrun exe examples-14-02withERROR: invalid input syntax for type integer: ""becausecolor_idheld a dangling pointer into a freedPGresultbuffer.Root cause
color_idwas extracted viastd.mem.span(c.PQgetvalue(res, 0, 0))inside ablk:block that also haddefer c.PQclear(res). The defer fires when the block exits — including onbreak :blk— so by the timecolor_idwas used in the inner loop, thePGresultbuffer it pointed into was already freed. On macOS, freed memory is zeroed, socolor_idread as"", and PostgreSQL rejected it as an invalid integer.Fix
Remove the
blk:scope. ExecutePQexecPreparedforinsert_cat_colorsdirectly in the outerinline forbody and deferPQclear(color_res)there, socolor_idstays valid for the full duration of the inner loop.